package com.ldj.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import com.ldj.common.SystemSession;
import com.ldj.hibernate.SuperviseOrgan;
import com.ldj.hibernate.SuperviseOrganDAO;
import com.ldj.hibernate.UserManagerScope;

public class SelectOrganLevelTag extends TagSupport{

	private static final long serialVersionUID = 1L;
	
	private String name;
	
	private String key;
	
	private String styleClass;
	
	private String firstValue;
	
	private String firstTitle;
	
	public int doStartTag() throws JspException{
		try{
			if(key == null)
				key = "";
			StringBuffer stringbuffer = new StringBuffer("");
			
//			stringbuffer.append(getLevel1Code(dataType));
			
			SuperviseOrganDAO dao = new SuperviseOrganDAO(null, null);
			List<UserManagerScope> infoList = dao.findBySupCode(SystemSession.getSupUserId((HttpServletRequest)pageContext.getRequest()));
			String value = "";
			if(infoList != null && infoList.size()>0){
				UserManagerScope info = infoList.get(0);
				value = info.getUserManagerScopeOrganId();
			}
			if(value == null)
				value = "";
			value = value.replace(",", "','");
			stringbuffer.append("<select id=\"" + name + "\" name=\"" + name + "\"");
			if(styleClass != null && !"".equals(styleClass)){
				stringbuffer.append(" class=\"" + styleClass + "\"");
			}
			stringbuffer.append(">\n");
			if(firstTitle != null && !"".equals(firstTitle)){
				if(firstValue == null)
					firstValue = "";
				stringbuffer.append("<option value=\"" + firstValue + "\">" + firstTitle + "</option>\n");
			}
			List list = dao.getSonOrgan(value);
			String t = "";
			if(list != null && list.size() > 0){
				for(int i = 0; i < list.size(); i ++){
					SuperviseOrgan s = (SuperviseOrgan)list.get(i);
					stringbuffer.append("<option value=\"" + s.getSupOrganCode() + "\"");
					if(key != null && key.equals(s.getSupOrganCode()))
						stringbuffer.append(" selected");
					stringbuffer.append(">");
					stringbuffer.append(s.getSupOrganName() + "</option>\n");
				}
			}
			stringbuffer.append("</select>\n");
			
			pageContext.getOut().write(stringbuffer.toString());
		}catch (IOException ex){
			throw new JspTagException("acmstag: " + ex.getMessage());
		} catch(Exception ex){
			throw new JspTagException("acmstag: " + ex.getMessage());
		}
		return SKIP_BODY;
	}
	
//	public String getLevel1Code(String value){
//		StringBuffer stringbuffer = new StringBuffer("");
//		SuperviseOrganDAO dao = new SuperviseOrganDAO(null, null);
//		stringbuffer.append("<select id=\"" + name + "\" name=\"" + name + "\"");
//		if(styleClass != null && !"".equals(styleClass)){
//			stringbuffer.append(" class=\"" + styleClass + "\"");
//		}
//		stringbuffer.append(">\n");
//		if(firstTitle != null && !"".equals(firstTitle)){
//			if(firstValue == null)
//				firstValue = "";
//			stringbuffer.append("<option value=\"" + firstValue + "\">" + firstTitle + "</option>\n");
//		}
//		List list = dao.getSonCatalog(value);
//		String t = "";
//		if(list != null && list.size() > 0){
//			for(int i = 0; i < list.size(); i ++){
//				SuperviseOrgan s = (SuperviseOrgan)list.get(i);
//				stringbuffer.append("<option value=\"" + s.getSupOrganCode() + "\"");
//				if(key != null && key.equals(s.getSupOrganCode()))
//					stringbuffer.append(" selected");
//				stringbuffer.append(">");
//				if(i == list.size() - 1){
//					stringbuffer.append("└");
//					t = "&nbsp;&nbsp;&nbsp;";
//				}else{
//					stringbuffer.append("├");
//					t = "│";
//				}
//				stringbuffer.append(s.getSupOrganName() + "</option>\n");
//				stringbuffer.append(getLevel2Code(s.getSupOrganCode(), t));
//			}
//		}
//		stringbuffer.append("</select>\n");
//		return stringbuffer.toString();
//	}
//	
//	public String getLevel2Code(String value, String isLast){
//		StringBuffer stringbuffer = new StringBuffer("");
//		SuperviseOrganDAO dao = new SuperviseOrganDAO(null, null);
//		List list = dao.getSonCatalog(value);
//		if(list != null && list.size() > 0){
//			for(int i = 0; i < list.size(); i ++){
//				String isLast1 = isLast;
//				SuperviseOrgan s = (SuperviseOrgan)list.get(i);
//				String code = s.getSupOrganCode();
//				stringbuffer.append("<option value=\"" + code + "\"");
//				if(key != null && key.equals(s.getSupOrganCode()))
//					stringbuffer.append(" selected");
//				stringbuffer.append(">");
//				stringbuffer.append(isLast);
//				if(i == list.size() - 1){
//					stringbuffer.append("└");
//					isLast1 += "&nbsp;&nbsp;&nbsp;";
//				}else{
//					stringbuffer.append("├");
//					isLast1 += "│";
//				}
//				stringbuffer.append(s.getSupOrganName() + "</option>\n");
//				stringbuffer.append(getLevel2Code(code, isLast1));
//			}
//		}
//		return stringbuffer.toString();
//	}

	public int doEndTag(){
		return EVAL_PAGE;
	}

	public String getFirstTitle() {
		return firstTitle;
	}

	public void setFirstTitle(String firstTitle) {
		this.firstTitle = firstTitle;
	}

	public String getFirstValue() {
		return firstValue;
	}

	public void setFirstValue(String firstValue) {
		this.firstValue = firstValue;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}
}
